load("//bazel:flags.bzl", "bool_flag", "selects", "string_flag_with_values")

licenses(["notice"])

# @platforms is found at https://github.com/bazelbuild/platforms
package(default_visibility = ["//:__subpackages__"])

config_setting(
    name = "linux_x64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:linux",
    ],
)

# Apple devices with intel processors released before the shift to the M1 chip
# will use this config setting.
config_setting(
    name = "mac_x64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:macos",
    ],
)

# M1 Macs (and later) will use this setting.
config_setting(
    name = "mac_arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        "@platforms//os:macos",
    ],
)

config_setting(
    name = "windows_x64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:windows",
    ],
)

config_setting(
    name = "linux_arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        "@platforms//os:linux",
    ],
)

config_setting(
    name = "debug_build",
    values = {"compilation_mode": "dbg"},
)

config_setting(
    name = "fast_build",
    values = {"compilation_mode": "fastbuild"},
)

config_setting(
    name = "release_build",
    values = {"compilation_mode": "opt"},
)

constraint_value(
    name = "fuchsia",
    constraint_setting = "@platforms//os:os",
)

config_setting(
    name = "fuchsia_arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        ":fuchsia",
    ],
)

# We define this here because the emscripten toolchain calls the cpu wasm, whereas the
# bazelbuild/platforms call it wasm32. https://github.com/emscripten-core/emsdk/issues/919
config_setting(
    name = "cpu_wasm",
    values = {
        "cpu": "wasm",
    },
)

selects.config_setting_group(
    name = "release_build_mac",
    match_all = [
        "@platforms//os:macos",
        ":release_build",
    ],
    visibility = ["//:__subpackages__"],
)

selects.config_setting_group(
    name = "release_build_linux",
    match_all = [
        "@platforms//os:linux",
        ":release_build",
    ],
    visibility = ["//:__subpackages__"],
)

selects.config_setting_group(
    name = "fast_build_mac",
    match_all = [
        "@platforms//os:macos",
        ":fast_build",
    ],
    visibility = ["//:__subpackages__"],
)

selects.config_setting_group(
    name = "fast_build_linux",
    match_all = [
        "@platforms//os:linux",
        ":fast_build",
    ],
    visibility = ["//:__subpackages__"],
)

# =============================================================================
#                     Configurable Skia Features
# =============================================================================
# These are flags that we can specify when invoking bazel build to turn on and
# off certain features, such as GPU backend, or codec support.
# https://bazel.build/rules/config#defining-build-settings
# For example, to enable harfbuzz and icu, one would run
# bazel build //:skia_public --//bazel/common_config_settings:use_harfbuzz \
#             --//bazel/common_config_settings:use_icu
# This is a bit wordy, so we define aliases in the //.bazelrc file that condense this to
# bazel build //:skia_public --with_harfbuzz --with_icu
#
# Developers can specify their own short-hands by making a .bazelrc file in their home
# directory. https://bazel.build/docs/bazelrc#bazelrc-file-locations
#
# We check in some Bazel configs for "blessed builds" in //bazel/buildrc.
#
# We can also define flags closer to where they have the most impact. For example
# //src/pdf:enable_pdf_backend.

string_flag_with_values(
    name = "fontmgr_factory",
    default = "empty_fontmgr_factory",
    values = [
        "android_fontmgr_factory",
        # Makes the default SkFontMgr load fonts from a hard-coded directory on disk.
        "custom_directory_fontmgr_factory",
        # Makes the default SkFontMgr load fonts from an SkEmbeddedResource that has been compiled
        # into the binary, e.g. with //tools/embed_resources.py
        "custom_embedded_fontmgr_factory",
        # Makes the default SkFontMgr return empty fonts (e.g. SkTypeface_Empty). This is typically
        # used when someone wants to make their own custom SkFontMgr objects, but does not want the
        # default SkFontMgr to do anything (e.g. force usage of the custom one).
        "custom_empty_fontmgr_factory",
        # Makes the default SkFontMgr return null. Typically used when font support is not desired.
        "empty_fontmgr_factory",
        "fontconfig_fontmgr_factory",
        # Deprecated, do not use.
        "fci_fontmgr_factory",
    ],
)

# These flags need only be set if additional functionality beyond the fontmgr_factory flag is
# required. For example, the setting fontmgr_factory to custom_embedded_fontmgr_factory does not
# require setting include_fontmgr to custom_embedded_fontmgr, because those sources and settings
# will already be compiled in due to the _factory flag.
string_flag_with_values(
    name = "include_fontmgr",
    multiple = True,
    values = [
        "android_fontmgr",
        # Allows the construction of an SkFontMgr that loads files from a programmatically
        # defined directory on disk.
        "custom_directory_fontmgr",
        # Allows the construction of an SkFontMgr which can load fonts from an SkEmbeddedResource
        # or from another source of raw bytes.
        "custom_embedded_fontmgr",
        # Allows the construction of an SkFontMgr which returns empty fonts.
        "custom_empty_fontmgr",
        "fontconfig_fontmgr",
        # Deprecated, do not use.
        "fci_fontmgr",
    ],
)

bool_flag(
    # This is necessary because Bazel provides no way to compile header files on their own
    name = "compile_generated_cpp_files_for_headers",
    default = False,
)

bool_flag(
    name = "enable_effect_serialization",
    default = True,
)

bool_flag(
    name = "enable_tracing",
    # See SkTraceEventCommon.h for more on this type of tracing.
    default = True,
)

bool_flag(
    name = "enable_skvm",
    default = False,
)

bool_flag(
    name = "use_harfbuzz",
    default = False,
)

bool_flag(
    name = "use_fontations",
    default = False,
)

bool_flag(
    name = "use_icu",
    default = False,
)

bool_flag(
    name = "build_for_debugger",
    default = False,
)

# These are some helpers to mean "either the fontmgr was enabled or its factory was"

selects.config_setting_group(
    name = "uses_android_fontmgr",
    match_any = [
        ":android_fontmgr",
        ":android_fontmgr_factory",
    ],
)

selects.config_setting_group(
    name = "uses_custom_directory_fontmgr",
    match_any = [
        ":custom_directory_fontmgr",
        ":custom_directory_fontmgr_factory",
    ],
)

selects.config_setting_group(
    name = "uses_custom_embedded_fontmgr",
    match_any = [
        ":custom_embedded_fontmgr",
        ":custom_embedded_fontmgr_factory",
    ],
)

selects.config_setting_group(
    name = "uses_custom_empty_fontmgr",
    match_any = [
        ":custom_empty_fontmgr",
        ":custom_empty_fontmgr_factory",
    ],
)

selects.config_setting_group(
    name = "uses_fontconfig_fontmgr",
    match_any = [
        ":fontconfig_fontmgr",
        ":fontconfig_fontmgr_factory",
    ],
)

selects.config_setting_group(
    name = "uses_fci_fontmgr",
    match_any = [
        ":fci_fontmgr",
        ":fci_fontmgr_factory",
    ],
)

# Convenience condition that is always true. This condition is satisfied if an arbitrarily chosen
# boolean built-in flag (https://bazel.build/docs/user-manual#stamp) is either true or false.
#
# Inspired by
# https://github.com/bazelbuild/bazel-skylib/blob/2f0bb4cec0297bb38f830a72fa8961bee057c3cd/lib/selects.bzl#L227.
selects.config_setting_group(
    name = "always_true",
    match_any = [
        ":always_true_0",
        ":always_true_1",
    ],
    visibility = ["//visibility:public"],
)

config_setting(
    name = "always_true_0",
    values = {"stamp": "0"},
)

config_setting(
    name = "always_true_1",
    values = {"stamp": "1"},
)
